<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--部门数据-->
            <el-col :span="4" :xs="24">
                <div class="head-container">
                    <el-input v-model="deptName" :placeholder="$locale('student.Enter')" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
                </div>
                <div class="head-container">
                    <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
                </div>
            </el-col>
            <!--用户数据-->
            <el-col :span="20" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
                    <el-form-item :label="$locale('teacher.add.Teacher Name')" prop="teacherName">
                        <el-input v-model="queryParams.teacherName" :placeholder="$locale('teacher.add.Please enter the teachers name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.add.state')" prop="orderStatus">
                        <el-select v-model="queryParams.orderStatus" :placeholder="$locale('geWudevice.add.state')" clearable style="width: 240px">
                            <el-option :label="$locale('order.add.processed')" :value="1" />
                            <el-option :label="$locale('order.add.untreated')" :value="0" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('teacher.add.Teachers email')" prop="contactEmail">
                        <el-input v-model="queryParams.contactEmail" :placeholder="$locale('teacher.add.Please enter the teachers email address')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" size="mini" @click="handleStatus" :disabled="ids.length == 0">{{ $locale('order.add.batch processing') }}</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="schoolDeviceList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column type="index" width="50">
                    </el-table-column>
                    <el-table-column :label="$locale('teacher.add.teacher')" align="center" key="createBy" prop="createBy">
                    </el-table-column>
                    <el-table-column :label="$locale('jobList.add.email')" align="center" key="contactEmail" prop="contactEmail" width="160">
                    </el-table-column>
                    <el-table-column :label="$locale('myClass.add.School Name')" align="center" key="schoolName" prop="schoolName">
                    </el-table-column>
                    <el-table-column sortable column-key="orderStatus" :filters="[{text: $locale('order.add.processed'), value: '1'}, {text: $locale('order.add.untreated'), value: '0'}]" :filter-method="filterHandler" :label="$locale('geWudevice.add.state')" align="center" key="orderStatus" prop="orderStatus">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.orderStatus?'success':'danger'" effect="dark">
                                {{ scope.row.orderStatus?$locale('order.add.processed'):$locale('order.add.untreated') }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('orderList.add.Order time')" align="center" key="createTime" prop="createTime">
                    </el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.device')" align="center" key="shopDetail" prop="shopDetail" width="250">
                    </el-table-column>
                    <el-table-column :label="$locale('classList.add.notes')" align="center" key="remark" prop="remark">
                        <template slot-scope="scope">
                            {{ scope.row.remark }}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleView(scope.row)">{{ $locale('geWudevice.view') }}</el-button>
                            <el-button size="mini" type="text" icon="el-icon-edit" :disabled="scope.row.orderStatus" @click="handleStatus(scope.row)">
                              {{ $locale('order.add.handle') }}</el-button>
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleQuotation(scope.row)">
                              {{ $locale('student.add.quotation') }}</el-button>
                            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button> -->
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <quotation ref="quotation" @getList="getList"></quotation>
        <viewDialog ref="view" />
    </div>
</template>

<script>
import { deleteById, list, setStatus, setStatusByIds } from "@/api/order/inquiry";
import { schoolSelect } from "@/api/experiment/experimentList";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import quotation from './add/index.vue'
import viewDialog from './add/view.vue'
import { getNames } from '@/utils/index'

export default {
    name: "inquiry",
    components: { Treeselect, quotation, viewDialog },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            schoolDeviceList: [],
            // 弹出层标题
            title: "",
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 部门名称
            deptName: undefined,
            // 表单参数
            defaultProps: {
                children: "children",
                label: "schoolName"
            },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                teacherName:'',
                orderStatus:'',
                contactEmail:''
            },
        };
    },
    watch: {
        // 根据名称筛选部门树
        deptName (val) {
            this.$refs.tree.filter(val);
        }
    },
    created () {
        this.getList();
        this.getDeptTree();
    },
    methods: {
        /** 查询用户列表 */
        getList () {
            this.loading = true;
            list(this.queryParams).then(({ data }) => {
                this.schoolDeviceList = data.records;
                this.total = data.total;
                this.loading = false;
            });
        },
        filterHandler (value, row, column) {
            const property = column['property'];
            return row[property] === value;
        },
        /** 查询部门下拉树结构 */
        getDeptTree () {
            schoolSelect().then(response => {
                this.deptOptions = response.data;
            });
        },
        // 筛选节点
        filterNode (value, data) {
            if (!value) return true;
            return data.schoolName.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick (data) {
            this.queryParams.schoolId = [data.id];
            this.handleQuery();
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.queryParams.schoolId = undefined;
            this.$refs.tree.setCurrentKey(null);
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleBatch () {
            list({ id: this.ids }).then(({ data }) => {
                this.$modal.msgSuccess("批量成功！");
                this.getList();
            });
        },
        // 查看
        handleView (row) {
            this.$refs.view.show(row)
        },
        /** 报价按钮操作 */
        handleQuotation (row) {
            this.$refs.quotation.handleUpdate(row)
        },
        handleStatus (row) {
            const Ids = row && row.id || this.ids;
            this.$modal.confirm('是否操作名称为"' + getNames(this.schoolDeviceList, Ids, 'createBy') + '"的数据项？').then(()=> {
                if (row && row.id) {
                    setStatus(Ids).then(() => this.getList())
                } else {
                    setStatusByIds(Ids).then(() => this.getList())
                }
            }).then((res) => {
                if (res.code == 200) this.$modal.msgSuccess("操作成功！");
            }).catch(() => { });
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除名称为"' + getNames(this.schoolDeviceList, ids, 'createBy') + '"的数据项？').then(function () {
                return deleteById({ id: ids });
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.id);
        },
    }
};
  </script>
    <style lang="scss" scoped>
::v-deep .el-table__row--level-1 .el-checkbox {
    display: none;
}
</style>
